<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>本地有约 - 忘记密码</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        :root {
            --primary-color: #8aa899;
            --primary-dark: #6a8a7a;
            --primary-light: #a8c0b0;
            --secondary-color: #f5f5f5;
            --text-color: #333;
            --text-light: #666;
            --border-color: #ddd;
            --error-color: #e57373;
            --success-color: #81c784;
            --shadow: 0 8px 24px rgba(0, 0, 0, 0.12);
            --radius: 12px;
        }

        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: "Segoe UI", "Microsoft YaHei", sans-serif;
        }

        body {
            background: linear-gradient(135deg, #f8fbf9 0%, #e8f0ec 100%);
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            padding: 20px;
        }

        .container {
            width: 100%;
            max-width: 420px;
        }

        .logo {
            text-align: center;
            margin-bottom: 40px;
        }

        .logo h1 {
            color: var(--primary-color);
            font-size: 36px;
            font-weight: 600;
            letter-spacing: 1px;
            margin-bottom: 12px;
        }

        .logo p {
            color: var(--text-light);
            font-size: 16px;
            font-weight: 400;
        }

        .card {
            background-color: white;
            border-radius: var(--radius);
            box-shadow: var(--shadow);
            padding: 40px 35px;
            margin-bottom: 25px;
            position: relative;
            overflow: hidden;
        }

        .card::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            height: 4px;
            background: linear-gradient(90deg, var(--primary-color), var(--primary-light));
        }

        .card-header {
            margin-bottom: 30px;
            text-align: center;
        }

        .card-header h2 {
            color: var(--text-color);
            font-size: 26px;
            font-weight: 600;
            margin-bottom: 10px;
        }

        .card-header p {
            color: var(--text-light);
            font-size: 15px;
        }

        .form-group {
            margin-bottom: 24px;
        }

        .form-group label {
            display: block;
            margin-bottom: 10px;
            font-size: 14px;
            color: var(--text-color);
            font-weight: 500;
        }

        .input-with-icon {
            position: relative;
        }

        .input-with-icon i {
            position: absolute;
            left: 16px;
            top: 50%;
            transform: translateY(-50%);
            color: var(--primary-color);
            font-size: 18px;
        }

        .input-with-icon input {
            padding-left: 50px;
        }

        .form-control {
            width: 100%;
            padding: 16px 18px;
            border: 2px solid var(--border-color);
            border-radius: 10px;
            font-size: 16px;
            transition: all 0.3s ease;
            background-color: #fafafa;
        }

        .form-control:focus {
            outline: none;
            border-color: var(--primary-color);
            background-color: white;
            box-shadow: 0 0 0 4px rgba(138, 168, 153, 0.15);
        }

        .btn {
            display: block;
            width: 100%;
            padding: 16px;
            background: linear-gradient(135deg, var(--primary-color), var(--primary-dark));
            color: white;
            border: none;
            border-radius: 10px;
            font-size: 16px;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.3s ease;
            box-shadow: 0 4px 12px rgba(138, 168, 153, 0.3);
        }

        .btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 6px 16px rgba(138, 168, 153, 0.4);
        }

        .btn-secondary {
            background: transparent;
            color: var(--primary-color);
            border: 2px solid var(--primary-color);
            box-shadow: none;
        }

        .btn-secondary:hover {
            background: rgba(138, 168, 153, 0.1);
            transform: translateY(-2px);
            box-shadow: 0 4px 12px rgba(138, 168, 153, 0.2);
        }

        .code-group {
            display: flex;
            gap: 12px;
        }

        .code-input {
            flex: 1;
        }

        .send-code-btn {
            width: 130px;
            white-space: nowrap;
            font-size: 14px;
        }

        .countdown {
            color: var(--primary-color);
            font-size: 13px;
            margin-top: 6px;
            text-align: right;
            font-weight: 500;
        }

        .links {
            text-align: center;
            margin-top: 25px;
        }

        .links a {
            color: var(--primary-color);
            text-decoration: none;
            font-size: 15px;
            font-weight: 500;
            transition: color 0.3s;
        }

        .links a:hover {
            text-decoration: underline;
            color: var(--primary-dark);
        }

        .message {
            padding: 14px 18px;
            margin-bottom: 24px;
            border-radius: 10px;
            text-align: center;
            animation: fadeIn 0.5s ease-out;
            font-size: 15px;
            font-weight: 500;
        }

        .error {
            background-color: rgba(229, 115, 115, 0.1);
            color: var(--error-color);
            border: 2px solid rgba(229, 115, 115, 0.3);
        }

        .success {
            background-color: rgba(129, 199, 132, 0.1);
            color: var(--success-color);
            border: 2px solid rgba(129, 199, 132, 0.3);
        }

        @keyframes fadeIn {
            from {
                opacity: 0;
                transform: translateY(-12px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }

        @media (max-width: 480px) {
            .card {
                padding: 30px 25px;
            }

            .logo h1 {
                font-size: 30px;
            }

            .card-header h2 {
                font-size: 22px;
            }

            .code-group {
                flex-direction: column;
            }

            .send-code-btn {
                width: 100%;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="logo">
            <h1>本地有约</h1>
            <p>重新开始您的活动之旅</p>
        </div>

        <div class="card">
            <div class="card-header">
                <h2>找回密码</h2>
                <p>重置密码，重新探索</p>
            </div>

            {% if error %}
            <div class="message error">{{ error }}</div>
            {% endif %}

            {% if success %}
            <div class="message success">{{ success }}</div>
            {% endif %}

            <form method="POST" action="{% url 'customer:forgot_password' %}">
                {% csrf_token %}
                <div class="form-group">
                    <label for="email">邮箱</label>
                    <div class="input-with-icon">
                        <i class="fas fa-envelope"></i>
                        <input type="email" id="email" name="email" class="form-control" placeholder="请输入注册邮箱" required>
                    </div>
                </div>

                <div class="form-group">
                    <label for="verification_code">验证码</label>
                    <div class="code-group">
                        <div class="input-with-icon code-input">
                            <i class="fas fa-shield-alt"></i>
                            <input type="text" id="verification_code" name="verification_code" class="form-control" placeholder="请输入验证码" required>
                        </div>
                        <button type="button" class="btn btn-secondary send-code-btn" id="sendCodeBtn">发送验证码</button>
                    </div>
                    <div class="countdown" id="countdown" style="display: none;"></div>
                </div>

                <div class="form-group">
                    <label for="new_password">新密码</label>
                    <div class="input-with-icon">
                        <i class="fas fa-lock"></i>
                        <input type="password" id="new_password" name="new_password" class="form-control" placeholder="请输入新密码" required>
                    </div>
                </div>

                <div class="form-group">
                    <label for="confirm_password">确认新密码</label>
                    <div class="input-with-icon">
                        <i class="fas fa-check-circle"></i>
                        <input type="password" id="confirm_password" name="confirm_password" class="form-control" placeholder="请再次输入新密码" required>
                    </div>
                </div>

                <button type="submit" class="btn">重置密码</button>
            </form>

            <div class="links">
                <a href="{% url 'customer:login' %}">返回登录</a>
            </div>
        </div>
    </div>

    <script>
    // 获取 CSRF 令牌的函数
    function getCSRFToken() {
        return document.querySelector('[name=csrfmiddlewaretoken]').value;
    }

    document.getElementById('sendCodeBtn').addEventListener('click', function() {
        const email = document.getElementById('email').value;
        const btn = this;

        if (!email) {
            alert('请输入邮箱地址');
            return;
        }

        // 验证邮箱格式
        const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
        if (!emailRegex.test(email)) {
            alert('请输入有效的邮箱地址');
            return;
        }

        // 发送验证码请求
        const formData = new FormData();
        formData.append('email', email);
        formData.append('csrfmiddlewaretoken', getCSRFToken());

        // 禁用按钮防止重复点击
        btn.disabled = true;
        btn.textContent = '发送中...';

        fetch('/customer/send-verification-code/', {
            method: 'POST',
            body: formData,
            headers: {
                'X-CSRFToken': getCSRFToken()
            }
        })
        .then(response => response.json())
        .then(data => {
            if (data.success) {
                alert('验证码已发送到您的邮箱');
                startCountdown(btn);
            } else {
                alert('发送失败: ' + data.message);
                btn.disabled = false;
                btn.textContent = '发送验证码';
            }
        })
        .catch(error => {
            console.error('Error:', error);
            alert('发送验证码时出现网络错误');
            btn.disabled = false;
            btn.textContent = '发送验证码';
        });
    });

    function startCountdown(btn) {
        let countdown = 60;
        const countdownElement = document.getElementById('countdown');

        btn.disabled = true;
        countdownElement.style.display = 'block';

        const timer = setInterval(() => {
            countdownElement.textContent = `${countdown}秒后重新发送`;
            countdown--;

            if (countdown < 0) {
                clearInterval(timer);
                btn.disabled = false;
                countdownElement.style.display = 'none';
                btn.textContent = '发送验证码';
            } else {
                btn.textContent = '重新发送';
            }
        }, 1000);
    }

    document.querySelector('form').addEventListener('submit', function(e) {
        e.preventDefault();

        const email = document.getElementById('email').value;
        const verificationCode = document.getElementById('verification_code').value;
        const newPassword = document.getElementById('new_password').value;
        const confirmPassword = document.getElementById('confirm_password').value;

        // 客户端验证
        if (!email) {
            alert('请输入邮箱地址');
            return false;
        }

        // 验证邮箱格式
        const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
        if (!emailRegex.test(email)) {
            alert('请输入有效的邮箱地址');
            return false;
        }

        if (!verificationCode) {
            alert('请输入验证码');
            return false;
        }

        if (!newPassword) {
            alert('请输入新密码');
            return false;
        }

        if (!confirmPassword) {
            alert('请确认新密码');
            return false;
        }

        if (newPassword !== confirmPassword) {
            alert('两次输入的密码不一致');
            return false;
        }

        if (newPassword.length < 4) {
            alert('密码长度至少为4位');
            return false;
        }

        // 显示加载状态
        const submitBtn = this.querySelector('button[type="submit"]');
        const originalText = submitBtn.textContent;
        submitBtn.disabled = true;
        submitBtn.textContent = '重置中...';

        // AJAX提交
        const formData = new FormData(this);

        fetch(this.action, {
            method: 'POST',
            body: formData,
            headers: {
                'X-Requested-With': 'XMLHttpRequest',
                'X-CSRFToken': getCSRFToken()
            }
        })
        .then(response => {
            if (!response.ok) {
                throw new Error('网络响应不正常');
            }
            return response.json();
        })
        .then(data => {
            if (data.success) {
                alert('密码重置成功！即将跳转到登录页面');
                setTimeout(() => {
                    window.location.href = '/customer/login/';
                }, 1500);
            } else {
                alert('重置失败: ' + data.message);
                submitBtn.disabled = false;
                submitBtn.textContent = originalText;
            }
        })
        .catch(error => {
            console.error('Error:', error);
            alert('网络错误，请重试');
            submitBtn.disabled = false;
            submitBtn.textContent = originalText;
        });
    });

    // 实时密码验证
    document.getElementById('confirm_password').addEventListener('input', function() {
        const newPassword = document.getElementById('new_password').value;
        const confirmPassword = this.value;
        const confirmInput = this;

        if (confirmPassword && newPassword !== confirmPassword) {
            confirmInput.style.borderColor = 'var(--error-color)';
            confirmInput.style.boxShadow = '0 0 0 4px rgba(229, 115, 115, 0.15)';
        } else if (confirmPassword && newPassword === confirmPassword) {
            confirmInput.style.borderColor = 'var(--success-color)';
            confirmInput.style.boxShadow = '0 0 0 4px rgba(129, 199, 132, 0.15)';
        } else {
            confirmInput.style.borderColor = 'var(--border-color)';
            confirmInput.style.boxShadow = 'none';
        }
    });

    // 输入框获取焦点时恢复默认样式
    const inputs = document.querySelectorAll('.form-control');
    inputs.forEach(input => {
        input.addEventListener('focus', function() {
            this.style.borderColor = 'var(--primary-color)';
            this.style.boxShadow = '0 0 0 4px rgba(138, 168, 153, 0.15)';
        });

        input.addEventListener('blur', function() {
            if (!this.value) {
                this.style.borderColor = 'var(--border-color)';
                this.style.boxShadow = 'none';
            }
        });
    });

    // 回车键提交表单
    document.addEventListener('keypress', function(e) {
        if (e.key === 'Enter') {
            const focusedElement = document.activeElement;
            if (focusedElement && focusedElement.form) {
                focusedElement.form.dispatchEvent(new Event('submit', { cancelable: true }));
            }
        }
    });
</script>
</body>
</html>